package com.hicham.ratroid;



import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.PorterDuff.Mode;
import android.hardware.Camera;
import android.os.Bundle;
import android.preference.PreferenceManager;

import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;


public class MainActivity extends Activity {
	final private static int DIALOG_LOGIN = 1;
	private Intent Client;
	public static Button BTNSTART;
	private Button btnSettings;
	private EditText ipfield, portfield;
	public static ProgressBar PB;
	private Camera camera;
	//private int cameraId = 0;
 

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

        Client = new Intent(this, Myservice.class);
        Client.setAction(MainActivity.class.getName());

        BTNSTART = (Button) findViewById(R.id.button1);
        btnSettings = (Button) findViewById(R.id.button2);
        ipfield = (EditText) findViewById(R.id.editText1);
        portfield = (EditText) findViewById(R.id.editText2);
        PB = (ProgressBar) findViewById(R.id.pb1);

        PB.getProgressDrawable().setColorFilter(Color.RED, Mode.SRC_IN);

       
   	/*	
    	if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
    	      Toast.makeText(this, "No camera on this device", Toast.LENGTH_LONG).show();
    	    } else {
    	      cameraId = findFrontFacingCamera();
    	      if (cameraId < 0) {
    	        Toast.makeText(this, "No front facing camera found.",
    	            Toast.LENGTH_LONG).show();
    	      } else {
    	        camera = Camera.open(cameraId);
    	        Log.i("cam opened","good");
    	      }
    	    }
    	Log.i("taking photo","good");
    	camera.takePicture(null, null,new PhotoHandler(this));
    	*/
    
    	btnSettings.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	
            	Intent intent = new Intent(getApplicationContext(),PrefActivity.class);
            	startActivity(intent);
            	
            }}); 
    	BTNSTART.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	String comm= (String) BTNSTART.getText();
            	if(comm.equals("Connect")){
            	Client.putExtra("IP", ipfield.getText().toString());
            	Client.putExtra("PORT", new Integer(portfield.getText().toString()));
            	
            	PB.setIndeterminate(true);
            	PB.setProgress(100);
            	
            	startService(Client);
                BTNSTART.setTextSize(15);
            	BTNSTART.setText("Disconnect");
            	}
            	if(comm.equals("Disconnect")){
            		stopService(Client);
            		PB.setIndeterminate(false);
            		PB.setProgress(100);
            		PB.getProgressDrawable().setColorFilter(Color.RED, Mode.SRC_IN);
            		PB.setProgress(100);
            		Toast.makeText(getApplicationContext(), "You have been Disconnected !", Toast.LENGTH_LONG).show();
            		BTNSTART.setTextSize(20);
            		BTNSTART.setText("Connect");
            	}
            }}); 
		
	}
	@Override
	public void onBackPressed() {
        moveTaskToBack(true);
        return;
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	private int findFrontFacingCamera() {
	    int cameraId = -1;
	    // Search for the front facing camera
	    int numberOfCameras = Camera.getNumberOfCameras();
	    for (int i = 0; i < numberOfCameras; i++) {
	      Camera.CameraInfo info = new Camera.CameraInfo();
	      Camera.getCameraInfo(i, info);
	      if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
	        cameraId = i;
	        break;
	      }
	    }
	    return cameraId;
	  }

	  @Override
	  protected void onPause() {
	    if (camera != null) {
	      camera.release();
	      camera = null;
	    }
	    super.onPause();
	  }
	  @Override
	  protected void onResume() {
		  super.onResume();
		  showDialog(DIALOG_LOGIN);
	  }
	  
	  @Override
	  protected Dialog onCreateDialog(int id) {

	   AlertDialog dialogDetails = null;

	   switch (id) {
	   case DIALOG_LOGIN:
	    LayoutInflater inflater = LayoutInflater.from(this);
	    View dialogview = inflater.inflate(R.layout.dialog_layout, null);

	    AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(this);
	    dialogbuilder.setTitle("Login");
	    dialogbuilder.setView(dialogview);
	    dialogDetails = dialogbuilder.create();

	    break;
	   }

	   return dialogDetails;
	  }

	  @Override
	  protected void onPrepareDialog(int id, Dialog dialog) {

	   switch (id) {
	   case DIALOG_LOGIN:
	    final AlertDialog alertDialog = (AlertDialog) dialog;
	    Button loginbutton = (Button) alertDialog
	      .findViewById(R.id.btn_login);
	    Button cancelbutton = (Button) alertDialog
	      .findViewById(R.id.btn_cancel);

	    final EditText password = (EditText) alertDialog
	      .findViewById(R.id.password);
	    

	    loginbutton.setOnClickListener(new View.OnClickListener() {

	     @Override
	     public void onClick(View v) {
	    	 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
			  String pass = prefs.getString("password", "admin");
			  if(pass.equals(password.getText().toString())){
				 password.setText("");
				 alertDialog.dismiss();
				 
			  }
			  else{
				  password.setText("");
				  password.requestFocus();
				  password.setError( "Please enter correct password!" );
				  }
	     }
	    });

	    cancelbutton.setOnClickListener(new View.OnClickListener() {

	     @Override
	     public void onClick(View v) {
	      moveTaskToBack(true);

	     }
	    });
	    alertDialog.setOnCancelListener(new OnCancelListener() {

	        public void onCancel(DialogInterface dialog) {
	        	moveTaskToBack(true);
	        }
	    });
	    break;
	   }
	  }
	 
}
